(function( $ ) {
	var dom_id
		//, states=['0_PAUSED','1_PLAYING','2_THUMB','3_ALBUM']
		, state=0
		, albumIndex=0
		, imageIndex=0
		, thumbPageIndex=0
		, albumPageIndex=0
		, nxtImageCache
		, crntImageCache
		, prevImageCache
		, allMetaDataFetched
		,albumsMetadata=[];
	$.fn.picaslide=function(ipoptions){
		var picaslidewrap = this;
		var options = {
			'transitionDelay': 3000
			, 'thumbsPerPage': 24
			, 'albumsPerPage': 8
			, 'isfscreen': false
			, 'firstScreen':'image' //'image' 'thumb' 'album'
		};
		if(ipoptions)$.extend(options, ipoptions);
		Utils.init(picaslidewrap, options);
	};
	
	var Utils = {
		 options:null
		 , utils:this
		 , init: function(dom, ipoptions){
			 dom_id = dom.attr('id')?dom.attr('id'):dom.attr('name')?dom.attr('id'):'';
			 options=ipoptions;
			 if(options.transitionDelay<=2999){
				 options.transitionDelay=3000;
			 }else if(options.transitionDelay>=10001){
				 options.transitionDelay=10000;
			 }
			 if(!dom_id || dom.data('initialized') || !options.albums)return false;
			 dom.append('<div id="'+dom_id+'_show_wait" class="'+dom_id+'_show_wait"></div><div id="'+dom_id+'_imageholder"><div id="'+dom_id+'_controls"><div id="'+dom_id+'_bt_play" class="'+dom_id+'_bt_play_paused"><span class="tooltip">Play/Pause Slideshow [Spacebar]</span></div><div id="'+dom_id+'_bt_time_plus"><span class="tooltip">Increase slideshow interval, maximum 10 seconds</span></div><div id="'+dom_id+'_bt_time_text"></div><div id="'+dom_id+'_bt_time_minus"><span class="tooltip">Decrease slideshow interval, minimum 3 seconds</span></div><div id="'+dom_id+'_bt_album"><span class="tooltip">Go to other albums</span></div><div id="'+dom_id+'_bt_thumb"><span class="tooltip">View thumbnails</span></div><div id="'+dom_id+'_bt_fscreen"><span class="tooltip">Fullscreen [F]</span></div><div id="'+dom_id+'_bt_left"></div><div id="'+dom_id+'_bt_right"></div><div id="'+dom_id+'_image_desc"><div id="'+dom_id+'_image_desc_text"></div><div id="'+dom_id+'_image_desc_nav_info"></div><div id="'+dom_id+'_image_exifs_handle">&nbsp;i&nbsp;</div><div id="'+dom_id+'_image_exifs"><div id="'+dom_id+'_image_exifs_descs"></div><div id="'+dom_id+'_image_exifs_vals"></div><div id="'+dom_id+'_bt_exifs_close"></div></div></div></div><div id="'+dom_id+'_image"></div><div id="'+dom_id+'_imagethumb_wrap"><div id="'+dom_id+'_imagethumb_header"><div id="'+dom_id+'_imagethumb_header_text"></div><div id="'+dom_id+'_bt_imagethumb_close"></div></div><div id="'+dom_id+'_imagethumb_list"></div><div id="'+dom_id+'_imagethumb_nxt"></div><div id="'+dom_id+'_imagethumb_navi"></div><div id="'+dom_id+'_imagethumb_prev"></div></div><div id="'+dom_id+'_albums_wrap"><div id="'+dom_id+'_albums_header"><div id="'+dom_id+'_albums_header_text">My Albums</div><div id="'+dom_id+'_bt_albums_close"></div></div><div id="'+dom_id+'_albums_list"></div><div id="'+dom_id+'_albums_nxt"></div><div id="'+dom_id+'_albums_navi"></div><div id="'+dom_id+'_albums_prev"></div></div></div>');
			 utils = this;
			 this.showWait(20000);
			 this.fetchMetaData(options.albums);
			 this.initListeners(options);
			 dom.data('initialized','yes');
			 if(options.firstScreen=='thumb'){
				 $("#"+dom_id+"_bt_thumb").click();
			 }
		 }
		 , showWait: function(delay){
			 var domshowwait = $("#"+dom_id+"_show_wait");
			 domshowwait.show();
			 setTimeout(function(){
				 domshowwait.hide();
			 },delay);
		 }
		 , fetchMetaData: function(albumurls){
			 var fetchedCnt = 0;
			 var fetchFailedCnt = 0;
			 $.each(albumurls, function(i,item){
				var url = item.replace('http://picasaweb.google.com/','').replace('https://picasaweb.google.com/','');
				var uName = url.substr(0, url.indexOf("/"));
				var aName = (url.indexOf('?authkey')>0)?url.substr(url.indexOf("/")+1, (url.indexOf("?")-(url.indexOf("/")+1))):url.substr(url.indexOf("/")+1,url.length);
				var auKey = (url.indexOf('?authkey')>0)?url.substr(url.indexOf("=")+1, (url.length-(url.indexOf("=")+1))):'';
				var jsonUrl = 'https://picasaweb.google.com/data/feed/api/user/' + uName + '/album/' + aName + '?' + ((auKey)?('&authkey=' + auKey):('')) + '&kind=photo&access=public&max-results=999&thumbsize=104u,640u,1280u&alt=json&reqId='+Math.round(new Date().getTime());
				$.ajax({
					url: jsonUrl
					, dataType: "json"
					, success: function(data){
						albumsMetadata[(i)]=data.feed;
						fetchedCnt++;
						if(fetchedCnt==(albumurls.length-fetchFailedCnt)){
							allMetaDataFetched=true;
							$('#'+dom_id+'_bt_album').show();
							if(options.firstScreen=='album'){
								$('#'+dom_id+'_bt_album').click();
							}
						}
						if(i==0){
							$("#"+dom_id+"_show_wait").hide();
							utils.renderCurrentAlbum();
						}
					}
					, error: function(xhr, status, error){
						albumsMetadata.splice(i,1);
						fetchFailedCnt++;
					}
				});
			 });
		 }
		 , renderCurrentAlbum: function(){
			 this.renderCurrentImage('first');
		 }
		 , renderCurrentImage: function(direction){//'prev' or 'next' or 'first'
			 var imagedom = $("#"+dom_id+"_image");
			 imagedom.empty().append('<img src = "resources/images/preload.gif"/>');
			 if('prev'==direction){
				 if(imageIndex==0){
					 imageIndex = albumsMetadata[albumIndex].entry.length;
				 }else if(imageIndex==1){
					 imageIndex = albumsMetadata[albumIndex].entry.length+1;
				 }
				 imageIndex--;
				 nxtImageCache = crntImageCache;
				 crntImageCache = prevImageCache;
				 utils.imagesLoaded(crntImageCache, function($images) {
					 imagedom.empty().append(this);
					 if(options.isfscreen){
						 options.isfscreen=false;
						 $("#"+dom_id+"_bt_fscreen").click();
					 }
				 });
				 prevImageCache = $('<img src = "'+albumsMetadata[albumIndex].entry[imageIndex-1].media$group.media$content[0].url+'"/>');
			 }else if('first'==direction){
				 crntImageCache = $('<img src = "'+albumsMetadata[albumIndex].entry[0].media$group.media$content[0].url+'"/>');
				 utils.imagesLoaded(crntImageCache, function($images) {
					 imagedom.empty().append(this);
					 if(options.isfscreen){
						 options.isfscreen=false;
						 $("#"+dom_id+"_bt_fscreen").click();
					 }
				 });
				 nxtImageCache = $('<img src = "'+albumsMetadata[albumIndex].entry[1].media$group.media$content[0].url+'"/>');
				 prevImageCache = $('<img src = "'+albumsMetadata[albumIndex].entry[albumsMetadata[albumIndex].entry.length-1].media$group.media$content[0].url+'"/>');
			 }else if('random'==direction){
				 crntImageCache = $('<img src = "'+albumsMetadata[albumIndex].entry[imageIndex].media$group.media$content[0].url+'"/>');
				 utils.imagesLoaded(crntImageCache, function($images) {
					 imagedom.empty().append(this);
					 if(options.isfscreen){
						 options.isfscreen=false;
						 $("#"+dom_id+"_bt_fscreen").click();
					 }
				 });
				 nxtImageCache = $('<img src = "'+albumsMetadata[albumIndex].entry[(imageIndex==albumsMetadata[albumIndex].entry.length-1)?0:(imageIndex+1)].media$group.media$content[0].url+'"/>');
				 prevImageCache = $('<img src = "'+albumsMetadata[albumIndex].entry[(imageIndex==0)?(albumsMetadata[albumIndex].entry.length-1):(imageIndex-1)].media$group.media$content[0].url+'"/>');
			 }else if('next'==direction){
				 if(imageIndex==(albumsMetadata[albumIndex].entry.length-1))imageIndex=-1;
				 imageIndex++;
				 prevImageCache = crntImageCache;
				 crntImageCache = nxtImageCache;
				 utils.imagesLoaded(crntImageCache, function() {
					 imagedom.empty().append(this);
					 if(options.isfscreen){
						 options.isfscreen=false;
						 $("#"+dom_id+"_bt_fscreen").click();
					 }
				 });
				 if(imageIndex>=(albumsMetadata[albumIndex].entry.length-1)){
					 imageIndex=-1;
				 }
				 nxtImageCache = $('<img src = "'+albumsMetadata[albumIndex].entry[imageIndex+1].media$group.media$content[0].url+'"/>');
			 }
			 utils.renderImageDetails(imageIndex);
		 }
		 , renderImageDetails: function(iIndex){
			 if(iIndex==-1){
				 iIndex=albumsMetadata[albumIndex].entry.length-1;
			 }else if(iIndex==albumsMetadata[albumIndex].entry.length){
				 iIndex=0;
			 }
			 $("#"+dom_id+"_image_desc_text")
			 	.empty()
			 	.html(albumsMetadata[albumIndex].entry[iIndex].summary.$t?utils.trimem(albumsMetadata[albumIndex].entry[iIndex].summary.$t, 175):albumsMetadata[albumIndex].entry[iIndex].title.$t);
			 $("#"+dom_id+"_image_desc_nav_info")
			 	.empty()
			 	.html(1+iIndex +' of '+albumsMetadata[albumIndex].entry.length);
			 var descs = $("#"+dom_id+"_image_exifs_descs").empty();
			 var vals = $("#"+dom_id+"_image_exifs_vals").empty();
			 var iseExists = false;
			 $.each(albumsMetadata[albumIndex].entry[iIndex].exif$tags, function(i, item){
				 var valDisp = '';
					if(i=='exif$focallength'){
						valDisp = item.$t + ' mm';
					}else if(i=='exif$exposure'){
						valDisp = utils.calcExifExposure(item.$t);
					}else if(i=='exif$make'
							|| i=='exif$model'
							|| i=='exif$distance'
							|| i=='exif$flash'
							|| i=='exif$iso'){
						valDisp = item.$t;
					}else if(i=='exif$time'){
						var d = new Date(Number(item.$t));
						//valDisp = d.getFullYear()+':'+(d.getMonth()+1)+':'+d.getDate()+'-'+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
						valDisp = d.toUTCString();
					}else{
						valDisp = '';
					}
					if(valDisp){
						descs.append('<div class="'+dom_id+'_image_exifs_desc">'+utils.capitalizeFirstLetter(i.replace('exif$',''))+'</div>');
						vals.append('<div class="'+dom_id+'_image_exifs_val">'+utils.trimem(valDisp,32)+'</div>');
						iseExists=true;
					}
			 });
			 if(iseExists){
				 $("#"+dom_id+"_image_exifs_handle").show();
			 }else{
				 $("#"+dom_id+"_image_exifs_handle").hide();
				 $("#"+dom_id+"_image_exifs").hide();
			 }
		 }
		 , manuallyHideControls: false
		 , initListeners: function(options){
			 //Show/Hide Controls
			 var controls = $("#"+dom_id+"_controls");
			 var imageholder = $("#"+dom_id+"_imageholder");
			 var bt_play = $("#"+dom_id+"_bt_play");
			 var bt_time_plus = $("#"+dom_id+"_bt_time_plus");
			 var bt_time_minus = $("#"+dom_id+"_bt_time_minus");
			 var bt_time_text = $("#"+dom_id+"_bt_time_text");
			 var bt_thumb = $("#"+dom_id+"_bt_thumb");
			 var bt_fscreen = $("#"+dom_id+"_bt_fscreen");
			 var bt_album = $("#"+dom_id+"_bt_album");
			 var bt_left = $("#"+dom_id+"_bt_left");
			 var bt_right = $("#"+dom_id+"_bt_right");
			 var imagethumb_wrap = $("#"+dom_id+"_imagethumb_wrap");
			 var exifs_handle = $("#"+dom_id+"_image_exifs_handle");
			 var imagethumb_close = $("#"+dom_id+"_bt_imagethumb_close");
			 var albums_wrap = $("#"+dom_id+"_albums_wrap");
			 var albums_close = $("#"+dom_id+"_bt_albums_close");
			 var exifs_close = $("#"+dom_id+"_bt_exifs_close");
			 var exifs_image = $("#"+dom_id+"_image_exifs");
			 var imagethumb_nxt = $("#"+dom_id+"_imagethumb_nxt");
			 var imagethumb_prev = $("#"+dom_id+"_imagethumb_prev");
			 var albums_nxt = $("#"+dom_id+"_albums_nxt");
			 var albums_prev = $("#"+dom_id+"_albums_prev");
			 var tooltips = $(".tooltip");
			 imageholder.hover(function(){
				if(state==0 || state==1){
					controls.animate({opacity: "show"}, "fast");
					controls.data('timeout', setTimeout(function(){
						controls.animate({opacity: "hide"}, "slow");
					}, 2000));
				}
			 }
			 , function(){
				 if(utils.manuallyHideControls){
					 controls.animate({opacity: "hide"}, "slow");
					 utils.manuallyHideControls=false;
				 }
			 });
			 controls.hover(function(){
				clearTimeout(controls.data('timeout'));
				utils.manuallyHideControls=true;
				//controls.delay(2000).animate({opacity: "hide"}, "slow");
			 });
			 //Show/Hide Play
			 bt_play.click(function(){
				 if(state==1){
					 $(this).addClass(dom_id+'_bt_play_paused').removeClass(dom_id+'_bt_play_playing');
					 state=0;//paused
					 clearTimeout(bt_play.data('playFn'));
					 $('#'+dom_id+'_bt_time_plus').hide();
					 $('#'+dom_id+'_bt_time_minus').hide();
					 $('#'+dom_id+'_bt_time_text').hide();
				 }else{
					 $(this).removeClass(dom_id+'_bt_play_paused').addClass(dom_id+'_bt_play_playing');
					 state=1;//playing
					 var playFn = setInterval(function(){
						 utils.renderCurrentImage('next');
					 },options.transitionDelay);
					 bt_play.data('playFn',playFn);
					 $('#'+dom_id+'_bt_time_plus').show();
					 $('#'+dom_id+'_bt_time_minus').show();
					 $('#'+dom_id+'_bt_time_text').show();
				}
			 });
			 //Show/Hide Thumbs
			 bt_thumb.click(function(){
				 if(state==2){
					 state=0;//paused
					 imagethumb_wrap.animate({opacity: "hide"}, "slow");
				 }else{
					 state=2;//thumbs
					 if(options.isfscreen){
						 var fScreenHeight = $(window).height();
						 var fScreenWidth = $(window).width();
						 var h = parseInt(imagethumb_wrap.css('height').replace('px',''));
						 var w = parseInt(imagethumb_wrap.css('width').replace('px',''));
						 imagethumb_wrap.css({'left':((fScreenWidth/2)-(w/2))+'px'
							 ,'top':((fScreenHeight/2)-(h/2))+'px'});
					 }else{
						 imagethumb_wrap.css({'left':''
							 ,'top':''});
					 }
					 imagethumb_wrap.animate({opacity: "show"}, "fast", function(){
						 thumbPageIndex = utils.showThumbs(thumbPageIndex);
						 controls.animate({opacity: "hide"}, "slow");
						 clearTimeout(bt_play.data('playFn'));
						 bt_play.addClass(dom_id+'_bt_play_paused').removeClass(dom_id+'_bt_play_playing');
					 });
					 var thumbRow=0;
					 var thumbColoum=65;
				 };
			 });
			 //Show/Hide Albums
			 bt_album.click(function(){
				if(state==3){
					state=0;
					albums_wrap.animate({opacity: "hide"}, "slow");
				}else{
					if(allMetaDataFetched){
						state=3;//albums
						if(options.isfscreen){
							 var fScreenHeight = $(window).height();
							 var fScreenWidth = $(window).width();
							 var h = parseInt(albums_wrap.css('height').replace('px',''));
							 var w = parseInt(albums_wrap.css('width').replace('px',''));
							 albums_wrap.css({'left':((fScreenWidth/2)-(w/2))+'px'
								 ,'top':((fScreenHeight/2)-(h/2))+'px'});
						 }else{
							 albums_wrap.css({'left':''
								 ,'top':''});
						 }
						albums_wrap.animate({opacity: "show"}, "fast", function(){
							albumPageIndex = utils.showAlbums(albumPageIndex);
							controls.animate({opacity: "hide"}, "slow");
							clearTimeout(bt_play.data('playFn'));
							bt_play.addClass(dom_id+'_bt_play_paused').removeClass(dom_id+'_bt_play_playing');
						});
					}
				}
			 });
			 //Toggle Fullscreen
			 bt_fscreen.click(function(){
				 var fScreenHeight = $(window).height();
				 var fScreenWidth = $(window).width();
				 var dom=$('#'+dom_id);
				 var domih=$('#'+dom_id+'_imageholder');
				 var domi=$('#'+dom_id+'_image');
				 var domii=$('#'+dom_id+'_image img');
				 var domc = $('#'+dom_id+'_controls');
				 var domid = $('#'+dom_id+ '_image_desc');
				 if(options.isfscreen){
					 options.isfscreen=false;
					 dom.css({
						 'width':''
						 , 'height':''
						 , 'top':''
						 , 'left':''
						 , 'position':''
					 });
					 domih.css({
						 'width':''
					 });
					 domi.css({
						 'width':''
					 });
					 domii.css({
						 'max-height':''
						 , 'max-width':''
					});
					domc.css({
						'height':''
					});
					domid.css({
						'bottom':''
					});
				 }else{
					 options.isfscreen=true;
					 dom.css({
						 'width':fScreenWidth+'px'
						 , 'height': (fScreenHeight-20)+'px'
						 , 'top':'0px'
						 , 'left':'0px'
						 , 'position':'absolute'
					 });
					 domih.css({
						 'width':fScreenWidth+'px'
					 });
					 domi.css({
						 'width':(fScreenWidth-4)+'px'
					 });
					 domii.css({
						 'max-height':(fScreenHeight-28)+'px'
						 , 'max-width':(fScreenWidth-4)+'px'
					});
					domc.css({
						'height':(fScreenHeight-15)+'px'
					});
					domid.css({
						'bottom':'10px'
					});
				 }
			 });
			 //Next/Prev Images
			 bt_left.click(function(){
				 bt_play.addClass(dom_id+'_bt_play_paused').removeClass(dom_id+'_bt_play_playing');
				 state=0;//paused
				 clearTimeout(bt_play.data('playFn'));
				 utils.renderCurrentImage('prev');
			 });
			 bt_right.click(function(){
				 bt_play.addClass(dom_id+'_bt_play_paused').removeClass(dom_id+'_bt_play_playing');
				 state=0;//paused
				 clearTimeout(bt_play.data('playFn'));
				 utils.renderCurrentImage('next');
			 });
			 //Show/Hide Exifs
			 exifs_handle.click(function(){
				 if(state==0||state==1){
					 exifs_image.toggle();
					 exifs_handle.toggleClass(dom_id+'_image_exifs_handle_selected');
				 }
			});
			 //Close buttons
			 imagethumb_close.click(function(){
				 imagethumb_wrap.animate({opacity: "hide"}, "slow");
				 controls.animate({opacity: "show"}, "fast");
				 controls.data('timeout', setTimeout(function(){
					controls.animate({opacity: "hide"}, "slow");
				 }, 2000));
				 state=0;
			 });
			 albums_close.click(function(){
				albums_wrap.animate({opacity: "hide"}, "slow");
				 controls.animate({opacity: "show"}, "fast");
				 controls.data('timeout', setTimeout(function(){
					controls.animate({opacity: "hide"}, "slow");
				 }, 2000));
				 state=0;
			 });
			 exifs_close.click(function(){
				 exifs_image.animate({opacity: "hide"}, "slow");
			 });
			//Prev/Next Buttons
			 imagethumb_nxt.click(function (){
				 thumbPageIndex = utils.showThumbs(++thumbPageIndex);
			});
			imagethumb_prev.click(function (){
				thumbPageIndex = utils.showThumbs(--thumbPageIndex);
			});
			//Prev/Next Buttons
			 albums_nxt.click(function (){
				 albumPageIndex = utils.showAlbums(++albumPageIndex);
			});
			albums_prev.click(function (){
				albumPageIndex = utils.showAlbums(--albumPageIndex);
			});
			//Plus Minus Timers
			bt_time_text.html(options.transitionDelay/1000);
			bt_time_plus.click(function(){
				if(options.transitionDelay<=9000){
					options.transitionDelay+=1000;
					bt_time_text.html(options.transitionDelay/1000);
					if(state==1){
						bt_play.click();
						bt_play.click();
					}
				}
			});
			bt_time_minus.click(function(){
				if(options.transitionDelay>=4000){
					options.transitionDelay-=1000;
					bt_time_text.html(options.transitionDelay/1000);
					if(state==1){
						bt_play.click();
						bt_play.click();
					}
				}
			});
			//Keyboard listeners
		   $(window).keydown(function(e) {
			   var ev = e || window.event;
		       var key = ev.keyCode || ev.which;
			   if(state==0 || state ==1){
			       if(key==37){//Left
			    	   bt_left.click();
			       }else if(key==39){//Right
			    	   bt_right.click();
			       }else if(key==32){//Spacebar
			    	   bt_play.click();
			       }else if(key==38){//Up
			    	   bt_album.click();
			       }else if(key==40){//Down
			    	   bt_thumb.click();
			       }
			   }else if(state==2 && key==27){//Esc
				   imagethumb_close.click();
			   }else if(state==3 && key==27){//Esc
				   albums_close.click();
			   }
			   if(key==70){
				   bt_fscreen.click();
			   }
		   });
		   $(window).keypress(function(e) {
			   if(state==0 || state ==1){
				   var ev = e || window.event;
			       var key = ev.keyCode || ev.which;
			       if(key==105){//i
			    	   var handle = $("#"+dom_id+"_image_exifs_handle");
			    	   if(handle.is(":visible")){
			    		   handle.click();
			    	   }
			       }
			   }
		   });
		   if(state==0){
			   $('#'+dom_id+'_bt_time_plus').hide();
			   $('#'+dom_id+'_bt_time_minus').hide();
			   $('#'+dom_id+'_bt_time_text').hide();
		   }
		   if(!allMetaDataFetched){
			   $('#'+dom_id+'_bt_album').hide();
		   }
		   tooltips.click(function(){
			   return false;
		   });
		 }
		 , showThumbs: function(i, isAlbum){
			 var container= $("#"+dom_id+"_imagethumb_list");
			 var container_header_text = $("#"+dom_id+"_imagethumb_header_text");
			 var navi = $("#"+dom_id+"_imagethumb_navi");
			 var totalPages = Math.ceil((albumsMetadata[albumIndex].entry.length) / utils.utils.options.thumbsPerPage);
			 i>=totalPages?i=(totalPages-1):i<=0?i=0:i=i;
			 var startIndex= (i) * utils.utils.options.thumbsPerPage;
			 var endIndex= ((i)*utils.utils.options.thumbsPerPage) + utils.utils.options.thumbsPerPage;
			 var thumbRow=0;
			 var thumbColoum=65;
			 if(i<totalPages){
				 container.empty();
				 container_header_text.empty().html(albumsMetadata[albumIndex].title.$t);
				 navi.empty().html((i+1) + ' of ' + totalPages);
			 }
			 container.hide();
			 $.each((albumsMetadata[albumIndex].entry).slice(startIndex, endIndex),function(i,item) {
				 var lPos= ((i%6==0)?(thumbRow=80):(thumbRow+=130));
				 var tPos= ((i>0&&i%6==0)?(thumbColoum+=95):(thumbColoum));
				 var imgDiv = $('<div class="'+dom_id+'_imagethumb_list_item"></div>')
				 	.css({'left':lPos,'top':tPos, 'cursor':'pointer'});
				 if((startIndex+i)==albumIndex){
					 imgDiv.css({'color':'#EEEEEE'});
				 }
				 imgDiv
				 	.append('<img src="'+item.media$group.media$thumbnail[0].url+'"/>')
				 	.data('navData',{index: startIndex+i})
				 	.click(function(){
			 			imageIndex = $(this).data('navData').index;
			 			$("#"+dom_id+"_bt_imagethumb_close").click();
			 			utils.renderCurrentImage('random');
				 	});
				 utils.imagesLoaded(imgDiv, function(){
					 container.append(this);
				 });
			 });
			 container.show();
			 return i;
		 }
		 , showAlbums: function(i){
			 var container=$("#"+dom_id+"_albums_list");
			 var navi = $("#"+dom_id+"_albums_navi");
			 var totalPages = Math.ceil((albumsMetadata.length) / utils.utils.options.albumsPerPage);
			 i>=totalPages?i=(totalPages-1):i<=0?i=0:i=i;
			 var startIndex= (i) * utils.utils.options.albumsPerPage;
			 var endIndex= ((i)*utils.utils.options.albumsPerPage) + utils.utils.options.albumsPerPage;
			 var thumbRow=0;
			 var thumbColoum=65;
			 if(i<totalPages){
				 container.empty();
				 navi.empty().html((i+1) + ' of ' + totalPages);
			 }
			 container.hide();
			 $.each(albumsMetadata.slice(startIndex, endIndex), function(j, item){
				 var lPos= ((j%2==0)?(thumbRow=80):(thumbRow+=380));
				 var tPos= ((j>0&&j%2==0)?(thumbColoum+=95):(thumbColoum));
				 var imgDiv = $('<div class="'+dom_id+'_albums_list_item"></div>')
				 	.css({'left':lPos,'top':tPos});
				 if((startIndex+j)==albumIndex){
					 imgDiv.css({'color':'#EEEEEE'});
				 }
				 imgDiv
				 	.append('<img src="'+item.icon.$t+'"/>')
				 	.append('<div class="'+dom_id+'_albums_title">'+item.title.$t+'<div class="'+dom_id+'_albums_desc">photos: '+item.gphoto$numphotos.$t+'</div></div>')
				 	.data('navData',{index: startIndex+j})
				 	.click(function(){
				 		albumIndex = $(this).data('navData').index;
				 		$("#"+dom_id+"_bt_albums_close").click();
				 		imageIndex=0;
			 			utils.renderCurrentImage('first');
			 			thumbPageIndex=0;
			 			albumPageIndex=0;
				 	});
				 utils.imagesLoaded(imgDiv, function(){
					 container.append(this);
				 });
			 });
			 container.show();
			 return i;
		 }
		 , capitalizeFirstLetter: function(input){
			 return input.charAt(0).toUpperCase() + input.slice(1);
		 }
		 , calcExifExposure: function(input){
			 if(typeof dec === 'string'){
				 return '1/'+Math.round(1/parseFloat(input)) + ' sec';
			 }else{
				 return '1/'+Math.round(1/input) + ' sec';
			 }
		 }
		 , timestamp: function() {
			 return new Date().getTime();
		 }
		 , trimem: function(s,e){
			 return (s.length>e)?(s.trim().substring(0, e).split(" ").slice(0, -1).join(" ") + "..."):s;
		 }
		 , imagesLoaded: function($this,callback) {
		    var $images = $this.find('img').add( $this.filter('img')),
	        len = $images.length,
	        blank = '';
		    function triggerCallback() {
		      callback.call( $this, $images );
		    }
		    function imgLoaded() {
		      if ( --len <= 0 && this.src !== blank ){
		        setTimeout( triggerCallback );
		        $images.unbind( 'load error', imgLoaded );
		      }
		    }
		    if ( !len ) {
		      triggerCallback();
		    }
		    $images.bind( 'load error',  imgLoaded ).each( function() {
		      if (this.complete || this.complete === undefined){
		        var src = this.src;
		        this.src = blank;
		        this.src = src;
		      }
		    });
		}
	 };
	
}(jQuery));